package ClassPackage;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author Coralie
 */
public class ControlerServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */

    Session hibernate=null;
    RequestDispatcher dispatcher;
    SessionFactory cfg;


    private void CreateFilm(String titre, int cost,Date dateSortie,String shortsum, String url_film, String url_small, String url_big,Set<Personne> director,Set<Personne> actor)
    {
        Film fil = new Film(titre, cost);

        if(dateSortie!=null)
            fil.setDateSortie(dateSortie);
        else
            fil.setDateSortie(new Date());

        if(shortsum!=null)
            fil.setShortSummary(shortsum);
        else
            fil.setShortSummary("");

        if(url_film!=null)
            fil.setUrl_film(url_film);
        else
            fil.setUrl_film("");

        if(url_small!=null)
            fil.setUrl_small_picture(url_small);
        else
            fil.setUrl_small_picture("");

         if(url_big!=null)
            fil.setUrl_big_picture(url_small);
        else
            fil.setUrl_big_picture("");

        if(director!=null)
        {
            fil.setDirector(director);
        }

        if(actor!=null)
        {
            fil.setActor(actor);
        }

        hibernate.persist(fil);
        hibernate.save(fil);
    }

   private void CreateFilm(String titre, int cost,Date dateSortie,String shortsum, String url_film, String url_small, String url_big,Personne director,Set<Personne> actor)
    {
        Film fil = new Film(titre, cost);

        if(dateSortie!=null)
            fil.setDateSortie(dateSortie);
        else
            fil.setDateSortie(new Date());

        if(shortsum!=null)
            fil.setShortSummary(shortsum);
        else
            fil.setShortSummary("");

        if(url_film!=null)
            fil.setUrl_film(url_film);
        else
            fil.setUrl_film("");

        if(url_small!=null)
            fil.setUrl_small_picture(url_small);
        else
            fil.setUrl_small_picture("");

          if(url_big!=null)
            fil.setUrl_big_picture(url_small);
        else
            fil.setUrl_big_picture("");

        if(director!=null)
        {
            fil.setDirector(director);
        }

        if(actor!=null)
        {
            fil.setActor(actor);
        }
        hibernate.persist(fil);
        hibernate.save(fil);
    }

   private void CreateFilm(String titre, int cost,Date dateSortie,String shortsum, String url_film, String url_small, String url_big,Personne director,Personne actor)
    {
        Film fil = new Film(titre, cost);

        if(dateSortie!=null) 
            fil.setDateSortie(dateSortie);
        else
            fil.setDateSortie(new Date());

        if(shortsum!=null)
            fil.setShortSummary(shortsum);
        else
            fil.setShortSummary("");

        if(url_film!=null)
            fil.setUrl_film(url_film);
        else
            fil.setUrl_film("");

        if(url_small!=null)
            fil.setUrl_small_picture(url_small);
        else
            fil.setUrl_small_picture("");

     if(url_big!=null)
            fil.setUrl_big_picture(url_small);
        else
            fil.setUrl_big_picture("");

        if(director!=null)
        {
            fil.setDirector(director);
        }

        if(actor!=null)
        {
            fil.setActor(actor);
        }
        hibernate.persist(fil);
         hibernate.save(fil);
    }

   private Film CreateFilm(String titre, int cost, Date dateSortie,String shortsum,String url_film, String url_small, String url_big)
   {
        Film fil = new Film(titre, cost);

        if(dateSortie!=null)
            fil.setDateSortie(dateSortie);
        else
            fil.setDateSortie(new Date());

        if(shortsum!=null)
            fil.setShortSummary(shortsum);
        else
            fil.setShortSummary("");

        if(url_film!=null)
            fil.setUrl_film(url_film);
        else
            fil.setUrl_film("");

        if(url_small!=null)
            fil.setUrl_small_picture(url_small);
        else
            fil.setUrl_small_picture("");

        if(url_big!=null)
            fil.setUrl_big_picture(url_small);
        else
            fil.setUrl_big_picture("");

        hibernate.persist(fil);
        hibernate.save(fil);
        return fil;
   }

   private Film CreateFilm(String titre, int cost,Genre gen, Date dateSortie,String shortsum,String url_film, String url_small, String url_big)
   {
        Film fil = new Film(titre, cost);

        if(dateSortie!=null)
            fil.setDateSortie(dateSortie);
        else
            fil.setDateSortie(new Date());

        if(shortsum!=null)
            fil.setShortSummary(shortsum);
        else
            fil.setShortSummary("");

        if(url_film!=null)
            fil.setUrl_film(url_film);
        else
            fil.setUrl_film("");

        if(url_small!=null)
            fil.setUrl_small_picture(url_small);
        else
            fil.setUrl_small_picture("");

        if(url_big!=null)
            fil.setUrl_big_picture(url_small);
        else
            fil.setUrl_big_picture("");

        if(gen !=null)
            fil.setGenre(gen);

        hibernate.persist(fil);
        hibernate.save(fil);
        return fil;
   }




    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
         if(hibernate==null)
            {

             cfg= new Configuration().configure().buildSessionFactory();

             
             /* hibernate= cfg.getCurrentSession();
              hibernate.getTransaction().begin();
                Genre gen = new Genre("Action");
               Genre gen1 = new Genre("Comique");
               Genre gen2 = new Genre("Horreur");
               Genre gen3 = new Genre("Drame");

               hibernate.saveOrUpdate(gen);
               hibernate.saveOrUpdate(gen1);
               hibernate.saveOrUpdate(gen2);
               hibernate.saveOrUpdate(gen3);

               hibernate.getTransaction().commit();
       */
/*
               Film atestailler = (Film)hibernate.get(Film.class,1);
               Utilisateur untutilise =(Utilisateur)hibernate.get(Utilisateur.class,1);
               untutilise.set(atestailler);
               hibernate.update(atestailler);
               hibernate.save(untutilise);
 * */
 
           
            }
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ControlerServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ControlerServlet at " + request.getContextPath () + "</h1>");
          
           try
            {
                int operation =Integer.parseInt(request.getParameter("Operation"));
                Film film;
                Utilisateur Util;
                
                switch (operation)
                {
                    case 0 :
                        ////////////////////////////////////////////////////////
                        //////////// CREATION D'UNE PERSONNE ///////////////////
                        ////////////////////////////////////////////////////////
                        try
                        {
                            hibernate= cfg.getCurrentSession();
                            hibernate.getTransaction().begin();
                            Personne ps= new Personne(request.getParameter("nom"),request.getParameter("prenom"));
                            hibernate.save(ps);
                            hibernate.getTransaction().commit();
                            dispatcher = this.getServletContext().getRequestDispatcher("/ajoutPer.jsp");
                            dispatcher.forward(request,response);
                        }
                        catch(HibernateException e)
                        {
                           if(hibernate.getTransaction() != null){
                               hibernate.getTransaction().rollback();
                           }

                            dispatcher = this.getServletContext().getRequestDispatcher("/ajoutPer.jsp");
                            dispatcher.forward(request,response);
                        }
                        break;
                        
                    case 1:
                        ////////////////////////////////////////////////////////
                        //////////// CREATION D'UN FILM      ///////////////////
                        ////////////////////////////////////////////////////////
                        try
                        {

                        hibernate= cfg.getCurrentSession();
                        hibernate.getTransaction().begin();

                        String title = request.getParameter("nomFilm");
                        int cost = Integer.parseInt(request.getParameter("jetonFilm"));
                        Date date = new Date(Date.parse(request.getParameter("dateFilm")));
                        String summ= request.getParameter("synopsis");
                        String url_film = request.getParameter("url_film");
                        String url_big = request.getParameter("afficheG");
                        String url_small = request.getParameter("afficheP");
                        String genre = request.getParameter("genre");

                        Genre gen ;
                        String test =request.getParameter("genre");
                        
                        if(test.compareTo("Action")==0)
                           gen= (Genre) hibernate.get(Genre.class,1);
                        else
                            if (test.compareTo("Comique")==0)
                                gen = (Genre) hibernate.get(Genre.class,2);
                            else
                                if (test.compareTo("Horreur")==0)
                                    gen =(Genre) hibernate.get(Genre.class,3);
                                else
                                    if (test.compareTo("Drame")==0)
                                        gen =(Genre)hibernate.get(Genre.class,4);
                                    else
                                        gen = new Genre();

                      
                        
                        film= CreateFilm(title, cost, date, summ, url_film, url_small, url_big);
                        film.setGenre(gen);
                        Personne act = new Personne();

                        request.setAttribute("Film_ID",Integer.toString(film.getID()));
                        request.setAttribute("Film",film);
                        request.setAttribute("Acteur",act);
                        request.setAttribute("ListePersonne", new ArrayList<Personne>());
                        request.setAttribute("SessionFact",cfg);
                        request.setAttribute("name","none");
                        request.setAttribute("Param_name","none");

                            hibernate.getTransaction().commit();
                            
                            hibernate= cfg.getCurrentSession();

                            hibernate.getTransaction().begin();
                           dispatcher = this.getServletContext().getRequestDispatcher("/ajoutActeur.jsp");
                           dispatcher.forward(request,response);

                        }
                        catch(HibernateException e)
                        {
                           if(hibernate.getTransaction() != null){
                               hibernate.getTransaction().rollback();
                           }

                            dispatcher = this.getServletContext().getRequestDispatcher("/ajoutFilm.jsp");
                            dispatcher.forward(request,response);
                        }
                        break;
                        
            
                    case 3:

                        ////////////////////////////////////////////////////////
                        //////////// CREATION D'UN UTILISATEUR//////////////////
                        ////////////////////////////////////////////////////////
                       
                       try{
                           hibernate= cfg.getCurrentSession();
                           hibernate.getTransaction().begin();
                           Utilisateur util = new Utilisateur(request.getParameter("nom"),request.getParameter("prenom"),request.getParameter("login"),request.getParameter("mpd"));
                           hibernate.save(util);
                           hibernate.getTransaction().commit();
                           dispatcher = this.getServletContext().getRequestDispatcher("/login.jsp");
                           dispatcher.forward(request,response);
                       }
                       catch(HibernateException e)
                       {
                           if(hibernate.getTransaction() != null){
                               hibernate.getTransaction().rollback();
                           }

                           dispatcher = this.getServletContext().getRequestDispatcher("/register.jsp");
                           dispatcher.forward(request,response);
                       }
                       break;
                        
                    case 4:
                        break;
                    case 5:
                        ////////////////////////////////////////////////////////
                        //////////// RECHERCHE DE FILM       ///////////////////
                        ////////////////////////////////////////////////////////
                         hibernate= cfg.getCurrentSession();
                         hibernate.getTransaction().begin();
                         film = new Film();
                         request.setAttribute("genre",request.getParameter("genre"));
                         request.setAttribute("real",request.getParameter("real"));
                         out.println("<h1>"+request.getParameter("genre")+"</h1>");
                         request.setAttribute("BeanFilm",film);
                         request.setAttribute("SessionFact",cfg);
                         request.setAttribute("Titre", request.getParameter("title"));
                         dispatcher = this.getServletContext().getRequestDispatcher("/search.jsp");
                         dispatcher.forward(request,response);

                        break;
                    case 6:

                        ////////////////////////////////////////////////////////
                        //////////// ACHAT DE JETONS         ///////////////////
                        ////////////////////////////////////////////////////////

                         hibernate= cfg.getCurrentSession();
                         hibernate.getTransaction().begin();

                         String log= request.getParameter("ID");
                         String nbj= request.getParameter("nbJeton");
                
                         Utilisateur ut = (Utilisateur)hibernate.get(Utilisateur.class, Integer.parseInt(log));
                         ut.setNbjetons(ut.getNbjetons()+Integer.parseInt(nbj));

                         out.println("<h1>"+"GRAOUUUU"+"</h1>");

                         hibernate.update(ut);

                         out.println("<h1>"+"LE RETOUR"+"</h1>");

                         hibernate.getTransaction().commit();

                          out.println("<h1>"+"ON a sauvegarde"+"</h1>");

                         Util = new Utilisateur();
                         Film filmUser= new Film();
                         request.setAttribute("Utilisateur",Util);
                         request.setAttribute("Film",filmUser);

                         request.setAttribute("SessionFact",cfg);

                         dispatcher = this.getServletContext().getRequestDispatcher("/perso.jsp");
                         dispatcher.forward(request,response);

                        break;
                    case 7:
                        break;
                    case 99:
                        ////////////////////////////////////////////////////////
                        //////////////          LOG IN            //////////////
                        ////////////////////////////////////////////////////////

                       hibernate= cfg.getCurrentSession();
                       hibernate.getTransaction().begin();

                       String login= request.getParameter("login");
                       String mdp = request.getParameter("mpd");
                        out.println("<h1>"+request.getParameter("login")+"</h1>");

                         out.println("<h1>"+request.getParameter("mpd")+"</h1>");

                       String My_Query ="from Utilisateur u where u.login = :log and u.password = :mdp";
                       Query query = hibernate.createQuery(My_Query);

                       query.setString("log",login);
                       query.setString("mdp", mdp);
                       
                       List<Utilisateur> results = query.list();
                       if(results.isEmpty())
                       {
                          dispatcher = this.getServletContext().getRequestDispatcher("/login.jsp");
                          dispatcher.forward(request,response);
                       }
                       else
                           if(results.size()==1)
                           { 
                              Iterator it = results.iterator();
                              Utilisateur tmp = (Utilisateur)it.next();
                              request.setAttribute("User_ID",Integer.toString(tmp.getID()));
                              request.setAttribute("login",login);
                              dispatcher = this.getServletContext().getRequestDispatcher("/saveName.jsp");
                              dispatcher.forward(request,response);
                           }
                          break;

                       case 150:

                        ////////////////////////////////////////////////////////
                        //////////////   PREPARATION RECHERCHE FILM/////////////
                        ////////////////////////////////////////////////////////
                             hibernate= cfg.getCurrentSession();
                             hibernate.getTransaction().begin();
                             film = new Film();

                            // List tttt= film.getListFilm(cfg,"none","none","none");

                            // out.println("<h1>"+tttt.size()+"</h1>");
                             request.setAttribute("real",null);
                             request.setAttribute("BeanFilm",film);
                             request.setAttribute("SessionFact",cfg);
                             request.setAttribute("genre",null);
                             request.setAttribute("Titre", "%");//request.getParameter("title"));
                             request.setAttribute("Realisateur","none");// request.getParameter("real"));

                             dispatcher = this.getServletContext().getRequestDispatcher("/search.jsp");
                             dispatcher.forward(request,response);
                           break;

                    case 250:
                        
                        ////////////////////////////////////////////////////////
                        ////    PREPARATION RECHERCHE ACTEUR/REALISATEUR    ////
                        ////////////////////////////////////////////////////////

                          try{

                           hibernate= cfg.getCurrentSession();
                           hibernate.getTransaction().begin();
                           Personne act = new Personne();
                           film = new Film();

                           request.setAttribute("Acteur", act);
                           request.setAttribute("name",request.getParameter("name"));
                           request.setAttribute("ListePersonne", new ArrayList<Personne>());
                           request.setAttribute("Film", film);
                           request.setAttribute("SessionFact",cfg);
                           request.setAttribute("Param_name","none");
                           request.setAttribute("Film_ID", request.getAttribute("Film_ID"));

                     
                           dispatcher = this.getServletContext().getRequestDispatcher("/ajoutActeur.jsp");
                           dispatcher.forward(request,response);
                       }
                       catch(HibernateException e)
                       {

                           if(hibernate.getTransaction() != null){
                               hibernate.getTransaction().rollback();
                           }

                           dispatcher = this.getServletContext().getRequestDispatcher("/ajoutActeur.jsp");
                           dispatcher.forward(request,response);
                       }
                       break;

                     default:
                    break;

                    case 400:
                        ////////////////////////////////////////////////////////
                        ////           AJOUT          ACTEUR/REALISATEUR    ////
                        ////////////////////////////////////////////////////////
                         try{

                         hibernate= cfg.getCurrentSession();
                         hibernate.getTransaction().begin();

                         String param =request.getParameter("param_name");
                         out.println("<h1>"+request.getParameter("param_name")+"</h1>");

                         List lst ;

                         hibernate.getTransaction().begin();
                     
                                 if(param.compareTo("none")!=0)
                                 {
                                     param = "%" + param + "%";
                                     String requete= "from Personne p where p.name like :aname";
                                     query = hibernate.createQuery(requete);
                                     query.setString("aname",param);
                                 }
                                 else
                                 {
                                    String requete= "from Personne";
                                    query = hibernate.createQuery(requete);
                                 }

                                 lst = query.list();

                                 if(lst.isEmpty())
                                 {
                                     // retourne une liste vide
                                     lst= new ArrayList();
                                 }
                               


                         out.println("<h1>FFFFFFFFFFFFF</h1>");
                         int t =Integer.parseInt(request.getParameter("ID_FILM"));
                         Film pointeur_film =(Film) hibernate.get ( Film.class ,t );

                         out.println("<h1>"+request.getParameter("choix")+"</h1>");
                         String A = "A";
                         String R = "R";

                         if(A.compareTo(request.getParameter("choix"))==0)
                         {
                            int indice =Integer.parseInt(request.getParameter("personnes"));
                            Personne tmp =(Personne)lst.get(indice-1);
                            Personne realone = (Personne)hibernate.get(Personne.class,tmp.getID());
                            pointeur_film.setActor(realone);
                            hibernate.update(pointeur_film);

                         }else
                             if(R.compareTo(request.getParameter("choix"))==0)
                             {
                                int indice =Integer.parseInt(request.getParameter("personnes"));
                                Personne tmp =(Personne)lst.get(indice-1);
                                Personne realone = (Personne)hibernate.get(Personne.class,tmp.getID());
                                pointeur_film.setDirector(realone);
                                hibernate.update(pointeur_film);

                             }

                              //hibernate.update(pointeur_film);
                               
                                hibernate.getTransaction().commit();

                                Personne act = new Personne();

                                request.setAttribute("Param_name",request.getParameter("param_name"));
                                request.setAttribute("Film_ID",Integer.toString(pointeur_film.getID()));
                                request.setAttribute("ListePersonne", new ArrayList<Personne>());
                                request.setAttribute("Film", pointeur_film);
                                request.setAttribute("Acteur", act);
                                request.setAttribute("SessionFact",cfg);
                                request.setAttribute("name","none");
 
                               out.println("<h1>teleportation</h1>");

                               hibernate= cfg.getCurrentSession();
                               hibernate.getTransaction().begin();
                               dispatcher = this.getServletContext().getRequestDispatcher("/ajoutActeur.jsp");
                               dispatcher.forward(request,response);

                        }
                        catch(HibernateException e)
                       {
                           out.println("<h1> "+ e +" </h1>");
                           if(hibernate.getTransaction() != null){
                               hibernate.getTransaction().rollback();
                           }

                        }
 
                        break;
                    case 600:
                         hibernate= cfg.getCurrentSession();
                         hibernate.getTransaction().begin();
                         Util = new Utilisateur();
                         filmUser= new Film();
                         request.setAttribute("Utilisateur",Util);
                         request.setAttribute("Film",filmUser);
                         request.setAttribute("SessionFact",cfg);

                         hibernate= cfg.getCurrentSession();
                         hibernate.getTransaction().begin();
                         
                         /*if(((String)request.getAttribute("loglog")).compareTo("admin")==0)
                                dispatcher= this.getServletContext().getRequestDispatcher("/index.jsp");
                         else*/
                                dispatcher= this.getServletContext().getRequestDispatcher("/perso.jsp");
                         dispatcher.forward(request, response);
                        break;

                    case 820:
                        ////////////////////////////////////////////////////////
                        ////    AFFICHAGE EN DETAIL FILMS                   ////
                        ////////////////////////////////////////////////////////

                        // hibernate= cfg.getCurrentSession();
                        // hibernate.getTransaction().begin();

                         film = new Film();
                         Utilisateur uttil= new Utilisateur();
                         request.setAttribute("Film",film);
                         request.setAttribute("Client", uttil);
                         request.setAttribute("ID_ID_Film",request.getParameter("id_flm"));
                         request.setAttribute("SessionFact",cfg);

                       //  out.println("<h1>"+request.getAttribute("ID_ID_Film")+"</h1>");
                         dispatcher = this.getServletContext().getRequestDispatcher("/film.jsp");
                         dispatcher.forward(request, response);

                        break;

                    case 1000:
                        ////////////////////////////////////////////////////////
                        ////    Ajout a la liste a voir                     ////
                        ////////////////////////////////////////////////////////
                        hibernate= cfg.getCurrentSession();
                        hibernate.getTransaction().begin();

                        uttil=(Utilisateur)hibernate.get(Utilisateur.class,Integer.parseInt(request.getParameter("_ID_User")) );
                        film =(Film) hibernate.get(Film.class,Integer.parseInt(request.getParameter("_ID_Film")));

                        if(uttil.getNbjetons()>= film.getCost())
                        {
                            try{
                            hibernate.getTransaction().begin();
                        
                            hibernate.persist(film);
                            hibernate.persist(uttil);

                            film.setNbachat(film.getNbachat()+1);
                            HashSet<Film> aset = new HashSet<Film>();
                            aset.add((Film)hibernate.get(Film.class,Integer.parseInt(request.getParameter("_ID_Film"))));
                            uttil.setFilm_a_voir(aset);
                            hibernate.update(uttil);
                            hibernate.update(film);
                            hibernate.getTransaction().commit();


                            hibernate= cfg.getCurrentSession();
                            hibernate.getTransaction().begin();
                            uttil.setNbjetons(uttil.getNbjetons()- film.getCost());
                            hibernate.update(uttil);
                            hibernate.getTransaction().commit();
                            
                            request.setAttribute("Utilisateur",uttil);
                            request.setAttribute("Film",film);
                            request.setAttribute("SessionFact",cfg);

                            //hibernate.update(film);
                            dispatcher = this.getServletContext().getRequestDispatcher("/perso.jsp");
                            dispatcher.forward(request, response);
                            }
                            catch(HibernateException e)
                            {
                                out.println(e);
                            }
                        }
                        else
                        {
                            request.setAttribute("real",null);
                            request.setAttribute("BeanFilm",film);
                            request.setAttribute("SessionFact",cfg);
                            request.setAttribute("genre",null);
                            request.setAttribute("Titre", request.getParameter("title"));
                            request.setAttribute("Realisateur", request.getParameter("real"));
                            
                            dispatcher = this.getServletContext().getRequestDispatcher("/search.jsp");
                            dispatcher.forward(request, response);
                        }
                        break;
                        
                         case 1200:
                        ////////////////////////////////////////////////////////
                        ////   suppression a la liste a voir  puis visionage////
                        ////////////////////////////////////////////////////////
                                hibernate= cfg.getCurrentSession();
                        hibernate.getTransaction().begin();

                        uttil=(Utilisateur)hibernate.get(Utilisateur.class,Integer.parseInt(request.getParameter("_ID_User")) );
                        film =(Film) hibernate.get(Film.class,Integer.parseInt(request.getParameter("_ID_Film")));

                        Set unSet = uttil.getFilm_a_voir();
                        boolean untest=false;

                        if(!unSet.isEmpty())
                        {
                            unSet.remove(film);

                        }

                        unSet = uttil.getFilm_vu();
                        unSet.add(film);

                        try
                        {
                            hibernate.update(uttil);
                        }
                        catch(HibernateException e)
                        {
                           out.println(e);
                        }

                        hibernate.getTransaction().commit();
                        dispatcher = this.getServletContext().getRequestDispatcher("/streaming.jsp");
                        dispatcher.forward(request, response);

                        break;

                }

               out.println("<h1>Film Sauvegardé</h1>");
             //  hibernate.getTransaction().commit();

                out.println("<h1>Validation finale</h1>");
            }
            catch (HibernateException e)
            {
               // if(hibernate.getTransaction() != null){
               //     hibernate.getTransaction().rollback();
               // }
                out.println(e);
            }
            out.println("</body>");
            out.println("</html>");
   
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
